package com.yiren.dbaa.module.exam.view.adapter;

import static com.bumptech.glide.request.RequestOptions.bitmapTransform;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.yiren.dbaa.R;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.net.bean.StudentBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by WANGRONGXIANG
 * on 2022/3/4
 * Describe 切换学员侧拉框--其它学员列表适配器
 */
public class StudentExamAdapter extends BaseAdapter {

    private Context context;
    private List<StudentBean> students;
    private OnStudentClickListener listener;

    private int currentType;
    private final static int TYPE_TITLE = 0;
    private final static int TYPE_LIST = 1;

    public StudentExamAdapter(Context context, List<StudentBean> students, OnStudentClickListener listener) {
        this.context = context;
        this.students = students;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        if (null == students || students.size() == 0) {
            return 0;
        } else {
            return students.size() + 1;
        }
    }

    @Override
    public Object getItem(int position) {
        return students.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 1) {
            return TYPE_TITLE;
        } else {
            return TYPE_LIST;
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        currentType = getItemViewType(position);
        if (null == convertView) {
            if (currentType == TYPE_TITLE) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_stu_title, null);
            } else {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_stu_exam, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            }
        } else {
            if (currentType == TYPE_LIST) {
                holder = (ViewHolder) convertView.getTag();
            }
        }
        if (currentType == TYPE_LIST) {
            int selectPosition;
            if (position == 0) {
                holder.llStuItem.setBackgroundColor(Color.parseColor("#E6F4FF"));
                selectPosition = 0;
            } else {
                holder.llStuItem.setBackgroundResource(R.drawable.item_background_layer);
                selectPosition = position - 1;
            }
            StudentBean s = students.get(selectPosition);
            Glide.with(context).load(AddressConstant.getStuPicUrl(s.getStudent())).apply(bitmapTransform(new CircleCrop())).placeholder(R.mipmap.default_icon).into(holder.ivStuIcon);
            if (s.getCode() != 0) {
                if (1 == s.getCode()) {
                    holder.tvStuTime.setVisibility(View.VISIBLE);
                    holder.tvStudyType.setText("(练车体验券)");
                    holder.tvStuName.setText("学员：" + s.getStudentName());
                    holder.tvStudyType.setTextColor(Color.parseColor("#3bc14e"));
                    holder.tvStuTime.setText("剩余时长" + s.getRemaining() + "分钟");
                } else if (2 == s.getCode()) {
                    holder.tvStudyType.setVisibility(View.GONE);
                    holder.tvStuTime.setVisibility(View.INVISIBLE);
                    holder.tvStuName.setText("测试练车");
                } else if (3 == s.getCode()) {
                    holder.tvStudyType.setText("(智能评判课程)");
                    holder.tvStuTime.setVisibility(View.VISIBLE);
                    holder.tvStuName.setText("学员：" + s.getStudentName());
                    holder.tvStudyType.setTextColor(Color.parseColor("#1f71eb"));
                    holder.tvStuTime.setText("剩余时长" + s.getRemaining() + "分钟");
                }
            } else {
                holder.tvStuName.setText("学员：" + s.getStudentName());
                holder.tvStudyType.setText("(自动播报练车)");
                holder.tvStudyType.setTextColor(Color.parseColor("#FFB625"));
                holder.tvStuTime.setVisibility(View.GONE);
            }
            holder.llStuItem.setOnClickListener(v -> {
                listener.onStudentClick(selectPosition);
            });
        }
        return convertView;
    }

    class ViewHolder {
        @BindView(R.id.ll_stu_item)
        LinearLayout llStuItem;
        @BindView(R.id.iv_stu_icon)
        ImageView ivStuIcon;
        @BindView(R.id.tv_stu_name)
        TextView tvStuName;
        @BindView(R.id.tv_study_type)
        TextView tvStudyType;
        @BindView(R.id.tv_stu_time)
        TextView tvStuTime;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface OnStudentClickListener {

        /**
         * 学员列表的点击事件
         *
         * @param position
         */
        void onStudentClick(int position);
    }

}
